import request from '@/utils/request'
import store from '../store'
import { getToken, getReToken } from '@/utils/auth'

export function login(username, password, randomStr, code) {
  var grant_type = 'password'
  var scope = 'server'
  return request({
    url: '/auth/oauth/token', // 请求路径
    headers: {
      'Authorization': 'Basic cGlnOnBpZw=='
    },
    method: 'post',
    params: { username, password, randomStr, code, grant_type, scope },
    notUserToken: true
  })
}

export function refreshToken() {
  var grant_type = 'refresh_token'
  // var refresh_token = store.getters.refresh_token
  var refresh_token = getReToken()
  return request({
    url: '/auth/oauth/token', // 请求路径
    headers: {
      'Authorization': 'Basic cGlnOnBpZw=='
    },
    method: 'post',
    params: { grant_type, refresh_token },
    notUserToken: true
  })
}

export function checkToken() {
  // var access_token = store.getters.access_token
  var access_token = getToken()
  return request({
    url: '/auth/oauth/check_token?token=' + access_token,
    method: 'get'
  })
}

export function mobileLogin(mobile, code) {
  var grant_type = 'mobile'
  var scope = 'server'
  return request({
    url: '/auth/mobile/token',
    headers: {
      'Authorization': 'Basic cGlnOnBpZw=='
    },
    method: 'post',
    params: { mobile, code, grant_type, scope }
  })
}

export function getInfo(token) {
  return request({
    url: '/admin/user/info',
    method: 'get'
  })
}

export function logout(accesstoken, refreshToken) {
  return request({
    url: '/auth/authentication/removeToken',
    method: 'post',
    params: { accesstoken, refreshToken }
  })
}
